<?php

namespace App\Listeners;

use App\Events\InviteUsersRegister;
use App\Invitation;
use App\User;
use Carbon\Carbon;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class AddVipToInviteUser
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     * 监听被邀请用户注册  给邀请人追加7天的VIP
     * @param  InviteUserRegister $event
     * @return void
     */
    public function handle(InviteUsersRegister $event)
    {
        //被邀请的用户ID
        $data['invited_user_id'] = $event->user->id;
        //邀请人用户ID
        $data['invite_user_id'] = deInvitationCode($event->user->invite_code);
        //邀请人用户Model
        $invite_userInfo = User::find($data['invite_user_id']);
        //是否VIP
        if ($invite_userInfo->is_vip == 0) {
            $vipduetime = Carbon::parse('+7 days')->toDateTimeString();
            $invite_userInfo->update(['is_vip' => 1, 'vipduetime' => $vipduetime]);
        } else {
            $vipduetime = Carbon::parse($invite_userInfo->vipduetime)->addDays(7);
            $invite_userInfo->update(['is_vip' => 1, 'vipduetime' => $vipduetime]);
        }
        Invitation::create($data);
    }
}
